package initialize

import (
	"net/http"

	// middleware "github.com/cn-joyconn/goadmin/middleware"
	auth "github.com/cn-joyconn/goadmin/middleware/auth"
	global "github.com/cn-joyconn/goadmin/models/global"
	routers "github.com/cn-joyconn/goadmin/routers"
	gologs "github.com/cn-joyconn/gologs"
	filetool "github.com/cn-joyconn/goutils/filetool"	
	"github.com/gin-gonic/gin"
)

//RegistorRouters 初始化总路由
func RegistorRouters(Router *gin.Engine) {
	selfDir := filetool.SelfDir()
	Router.StaticFS(global.AppConf.ContextPath+"/static", http.Dir(selfDir+"/static/")) // 提供静态文件地址

	contextRouter := Router.Group(global.AppConf.ContextPath)
	// 方便统一添加路由组前缀 多服务器上线使用
	PublicGroup := contextRouter.Group("")
	AuthGroup := contextRouter.Group("")
	PermissioneGroup := contextRouter.Group("")
	AuthGroup.Use(auth.Authorize(global.AppConf.Authorize.LoginUrl))
	PermissioneGroup.Use(auth.Permission())
	JoyAuthorizeGroup := &auth.JoyAuthorizeGroup{GinGroup: AuthGroup}
	JoyPermissionGroup := &auth.JoyPermissionGroup{GinGroup: PermissioneGroup}

	routers.InitCommonRouter(PublicGroup, JoyAuthorizeGroup, JoyPermissionGroup)
	routers.InitAccountRouter(PublicGroup, JoyAuthorizeGroup, JoyPermissionGroup)
	routers.InitHomeRouter(PublicGroup, JoyAuthorizeGroup, JoyPermissionGroup)
	routers.InitAdminResourceRouter(PublicGroup, JoyAuthorizeGroup, JoyPermissionGroup)
	routers.InitAdminRoleRouter(PublicGroup, JoyAuthorizeGroup, JoyPermissionGroup)
	routers.InitAdminMenuRouter(PublicGroup, JoyAuthorizeGroup, JoyPermissionGroup)
	routers.InitAdminUserRouter(PublicGroup, JoyAuthorizeGroup, JoyPermissionGroup)
	routers.InitAdminAppRouter(PublicGroup, JoyAuthorizeGroup, JoyPermissionGroup)


	JoyPermissionGroup.SaveAllPermissionToDB()
	gologs.GetLogger("").Info("router register success")
}
